<template>
  <div>
    <el-dialog :title="title" :closeOnClickModal="false" :visible.sync="show" width="650px">
      <el-form label-position="right" :model="selectTable" ref="addForm" label-width="80px" :inline="false" autocomplete="off">
        <el-row>
          <el-col :span="16">
            <el-form-item label="站点名称" prop="siteName" :rules="[rule[0],{ min: 3, message: '最少输入3个字符', trigger: 'blur'}]">
              <el-input placeholder="请输入站点名称" v-model="selectTable.siteName" maxLength="20" style="width:200px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="siteStatus" :rules="[rule[0]]">
              <el-switch v-model="selectTable.siteStatus" :inactive-value="1" :active-value="0" active-text="启用" inactive-text="停用">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="一级分类" prop="parentType">
              <el-select v-model="selectTable.parentType" placeholder="请选择" @change="changeParentType()">
                <el-option value="">请选择</el-option>
                <el-option v-for="(value, key) in parentType" :key="key" :label="value.name" :value="value.value">{{value.name}}</el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="selectTable.parentType" label="二级分类" prop="site_type">
              <el-select v-model="selectTable.siteType" placeholder="请选择">
                <el-option v-for="(value, key) in siteType[selectTable.parentType]" :key="key" :label="value.name" :value="value.value">{{value.name}}</el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="comment">
              <el-input placeholder="请输入备注" v-model="selectTable.comment" maxLength="50"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="功能" prop="ids" :rules="[rule[0]]">
              <div class="scroller">
                <el-scrollbar>
                  <el-tree ref="tree" :data="funcData" @check-change="checkChange" default-expand-all :check-strictly="true" :expand-on-click-node="false" show-checkbox node-key="id" :default-checked-keys="selectTable.ids" :props="{
                  children: 'children',
                  label: 'functionName'
                }">
                    <span class="custom-tree-node" slot-scope="{ node, data }" @click="checkClick(node)">
                      <span>{{ node.label }}</span>
                      <span>{{data.functionType===1?data.functionUri:data.functionCode}}</span>
                    </span>
                  </el-tree>
                </el-scrollbar>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style lang="scss" scoped>
.scroller {
  border: 1px #e4e4e4 solid;
  height: 250px;
  overflow: hidden;
  .el-scrollbar {
    height: 267px;
  }
}
</style>

<script>
import * as api from '@/utils/api'
export default {
  data() {
    return {
      pload: true,
      rule: [
        { required: true, message: '必填项', trigger: 'change' }
      ],
      show: true,
      funcData: [],
      parentType: [{
        value: 3,
        name: '产业金融'
      }, {
        value: 4,
        name: '产业经济'
      }, {
        value: 5,
        name: '产业服务'
      }],
      siteType: {
        3: [{
          value: 15,
          name: '产业金融'
        }],
        5: [{
          value: 16,
          name: '产业服务'
        }],
        4: [{
          value: 7,
          name: '能源'
        }, {
          value: 8,
          name: '化工'
        }, {
          value: 9,
          name: '有色'
        }, {
          value: 10,
          name: '钢铁'
        }, {
          value: 11,
          name: '橡塑'
        }, {
          value: 12,
          name: '建材'
        }, {
          value: 13,
          name: '农副'
        }, {
          value: 14,
          name: '纺织'
        }]
      },
      selectTable: {}
    }
  },
  props: {
    data: Object
  },
  created() {
    this.selectTable = this.JSONs(this.data)
    this.selectTable.ids = this.selectTable.children
    this.getData()
  },
  watch: {
    show(newVal) {
      this.$parent.edit = false
    }
  },
  computed: {
    title() {
      if (this.selectTable.method === 'edit') {
        return '修改'
      } else if (this.selectTable.method === 'add') {
        return '添加'
      }
    }
  },
  methods: {
    checkChange(data, b) {
      if (b) {
        // 自己是选中状态
        this.$refs.tree.setChecked(data.parentFunctionId, true)
        data.children.map(item => {
          console.log(item.id)
          this.$refs.tree.setChecked(item.id, true)
        })
      } else {
        // 自己是非选中状态， 清除子节点选中
        data.children.map(item => {
          this.$refs.tree.setChecked(item.id, false)
        })
      }
      this.selectTable.ids = this.$refs.tree.getCheckedKeys()
    },
    changeParentType() {
      this.selectTable.siteType = this.siteType[this.selectTable.parentType][0].value
    },
    checkClick(data) {
      if (!data.checked) {
        this.$refs.tree.setChecked(data.parentFunctionId, true)
      }
      this.$refs.tree.setChecked(data, !data.checked)
    },
    async getData() {
      const Datas = await api.funcLibListOk({})
      this.funcData = Datas
      this.pload = false
    },
    async submitForm(formName) {
      await this.$refs[formName].validate()
      try {
        this.pload = true
        const data = this.JSONs(this.selectTable)
        data.ids = data.ids.join(',')
        delete data.children
        if (data.method === 'edit') {
          await api.siteModify(data)
        } else if (data.method === 'add') {
          await api.siteSave(data)
        }
        this.$notify({ title: '操作成功', type: 'success', duration: 5000 })
        this.$parent.edit = false
        this.$emit('getData')
      } catch (e) {
        this.pload = false
      }
    }
  }
}
</script>